import { reactive, Ref, ref } from "vue";

export function useTableFn<P,R extends {list:any,total:number}>(reqAPI:(params:P,id?:number)=>TRes<R>, reqParams: P,id?:any) {
  // 用户列表数据
  const userList: Ref<R['list']> = ref([]);
  // 列表请求参数
  const paginationParams = reactive({
    currentPage4: 1,
    pageSize4: 5,
    total: 1,
  });
  // 列表数据请求
  const queryList = async (qid?:number,params?:object) => {
    let res = await reqAPI({
      pageNum: paginationParams.currentPage4,
      pageSize: paginationParams.pageSize4,
      ...reqParams,
      ...params
    },qid||id);
    console.log(res);
    userList.value = res.data.list;
    paginationParams.total = res.data.total;
  };
  queryList();
  // 分页器
  function handleSizeChange(val: number) {
    queryList();
  }

  function handleCurrentChange(val: number) {
    queryList();
  }

  return {
    userList,
    paginationParams,
    handleSizeChange,
    handleCurrentChange,
    queryList,
  };
}
